package cn.itcast.shop.order.dao;

import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.itcast.shop.categorysecond.vo.CategorySecond;
import cn.itcast.shop.order.vo.OrderItem;
import cn.itcast.shop.order.vo.Orders;
import cn.itcast.shop.utils.PageHibernateCallback;

public class OrdersDao extends HibernateDaoSupport {

	public void saveOrder(Orders orders) {
		this.getHibernateTemplate().save(orders);		
	}

	/**
	 * 我的订单个数统计
	 * @param uid
	 * @return
	 */
	public Integer findByCountUid(Integer uid) {
		String hql = "select count(*) from Orders o where o.user.uid=?";
		@SuppressWarnings("unchecked")
		List<Long> list = this.getHibernateTemplate().find(hql,uid);
		if (list!=null && list.size()>0) {
			return list.get(0).intValue();
		}
		return null;
	}

	/**
	 * 我的订单列表查询
	 * @param uid
	 * @param begin
	 * @param limit
	 * @return
	 */
	public List<Orders> findByPageUid(Integer uid, int begin, Integer limit) {
		String hql = "from Orders o where o.user.uid=? order by ordertime desc";
		List<Orders> list =this.getHibernateTemplate().execute(new PageHibernateCallback<Orders>(hql,new Object[]{uid},begin,limit));
		
		return list;
	}

	/**
	 * 根据订单ID查询订单信息
	 * @param oid
	 * @return
	 */
	public Orders findByOid(Integer oid) {
		return this.getHibernateTemplate().get(Orders.class, oid);
	}

	public void update(Orders currOrder) {
		this.getHibernateTemplate().update(currOrder);
	}

	public int findCount() {
		String hql = "select count(*) from Orders";
		@SuppressWarnings("unchecked")
		List<Long> list = this.getHibernateTemplate().find(hql);
		if (list!=null&&list.size()>0) {
			return list.get(0).intValue();
		}
		return 0;
	}

	public List<Orders> findByPage(int begin, Integer limit) {
		String hql = "from Orders order by ordertime desc";
		List<Orders> list = this.getHibernateTemplate().execute(new PageHibernateCallback<Orders>(hql, null, begin, limit));
		
		if (list!=null&&list.size()>0) {
			return list;
		}
		return null;
	}

	/**
	 * 根据订单ID查询订单项
	 * @param oid
	 * @return
	 */
	public List<OrderItem> findOrderItem(Integer oid) {
		String hql = "from OrderItem oi where oi.orders.oid=?";
		@SuppressWarnings("unchecked")
		List<OrderItem> list = this.getHibernateTemplate().find(hql,oid);
		
		if (list!=null&&list.size()>0) {
			return list;
		}
		return null;
	}
	
}





























